package com.tanhua.manage.service;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tanhua.manage.domain.AnalysisByDay;
import com.tanhua.manage.mapper.AnalysisByDayMapper;
import com.tanhua.manage.mapper.LogMapper;
import com.tanhua.manage.vo.AnalysisSummaryVo;
import com.tanhua.manage.utils.ComputeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class AnalysisByDayService extends ServiceImpl<AnalysisByDayMapper, AnalysisByDay> {

    @Autowired(required = false)
    private LogMapper logMapper;

    /**
     * 首页面板统计
     *
     * @return
     */
    public ResponseEntity<Object> summary() {
        // 1.累计用户数
        AnalysisByDay analysisByDay = query().select("sum(num_registered) as num_registered").one();
        Integer numRegistered = analysisByDay.getNumRegistered();

        // 2.查询今日新增数、活跃用户数、登录次数
        Date date = new Date();
        // 获取今日的日期
        String now = new SimpleDateFormat("yyyy-MM-dd").format(date);
        // 获取今日的统计数据
        AnalysisByDay recordToday = query().eq("record_date", now).one();
        // 今日新增用户数
        Integer numRegisteredToday = recordToday.getNumRegistered();
        // 今日活跃用户数
        Integer numActiveToday = recordToday.getNumActive();
        // 今日登录次数
        Integer numLoginToday = recordToday.getNumLogin();

        // 3.查询昨日新增数、活跃用户数、登录次数
        // 获取昨天的日期
        String yes = ComputeUtil.offsetDay(date, -1);
        // 获取昨日的统计数据
        AnalysisByDay recordYes = query().eq("record_date", yes).one();
        Integer numRegisteredYes = 0;
        Integer numActiveYes = 0;
        Integer numLoginYes = 0;
        if( recordYes != null ){
            // 昨日新增用户数
            numRegisteredYes = recordYes.getNumRegistered();
            // 昨日活跃用户数
            numActiveYes = recordYes.getNumActive();
            // 昨日登录次数
            numLoginYes = recordYes.getNumLogin();
        }



        // 4.查询过去7天和过去30天活跃用户数
        Long day7 = this.getBaseMapper().findNumActiveByDate(ComputeUtil.offsetDay(date, -7), now);
        Long day30 = this.getBaseMapper().findNumActiveByDate(ComputeUtil.offsetDay(date, -30), now);

        // 5.封装返回结果
        AnalysisSummaryVo vo = new AnalysisSummaryVo();
        // 累计用户数
        vo.setCumulativeUsers(numRegistered.longValue());
        // 过去30天活跃
        vo.setActivePassMonth(day30);
        // 过去7天活跃
        vo.setActivePassWeek(day7);
        // 今日新增用户数
        vo.setNewUsersToday(numRegisteredToday.longValue());
        // 今日新增用户涨跌率
        vo.setNewUsersTodayRate(ComputeUtil.computeRate(numRegisteredToday, numRegisteredYes));
        // 今日登录数
        vo.setLoginTimesToday(numLoginToday.longValue());
        // 今日登录涨跌率
        vo.setLoginTimesTodayRate(ComputeUtil.computeRate(numLoginToday, numLoginYes));
        // 今日活跃用户数量
        vo.setActiveUsersToday(numActiveToday.longValue());
        // 今日活跃用户涨跌率
        vo.setActiveUsersTodayRate(ComputeUtil.computeRate(numActiveToday, numActiveYes));

        return ResponseEntity.ok(vo);
    }

    /**
     * 统计数据
     */
    public void analysis() {
        // 1.查询日志表，得到今日新增数、今日登录数、今日活跃数、今日留存数
        // 1.1 得到今日的日期，年月日
        Date date = new Date();
        String now = new SimpleDateFormat("yyyy-MM-dd").format(date);

        // 今日新增数
        Long newUserCount = logMapper.queryNumsByType(now, "0102");
        // 今日登录数
        Long loginCount = logMapper.queryNumsByType(now, "0101");
        // 今日活跃用户数
        Long activeCount = logMapper.queryNumsByDate(now);
        // 次日留存数
        Long retention1d = logMapper.queryRetention1d(now, ComputeUtil.offsetDay(date, -1));

        // 2.查询今日的统计数据是否存在，如果存在就更新，否则就新增
        AnalysisByDay recordToday = query().eq("record_date", now).one();

        if(recordToday == null){
            // 新增
            recordToday = new AnalysisByDay();
            // 日期必须只包含年月日
            Date dateWithYearMonthDay = DateUtil.parse(now);
            recordToday.setRecordDate(dateWithYearMonthDay);
            recordToday.setNumRegistered(newUserCount.intValue());
            recordToday.setNumActive(activeCount.intValue());
            recordToday.setNumLogin(loginCount.intValue());
            recordToday.setNumRetention1d(retention1d.intValue());
            recordToday.setCreated(new Date());
            recordToday.setUpdated(new Date());

            save(recordToday);

        }else{
            // 更新
            recordToday.setNumRegistered(newUserCount.intValue());
            recordToday.setNumActive(activeCount.intValue());
            recordToday.setNumLogin(loginCount.intValue());
            recordToday.setNumRetention1d(retention1d.intValue());
            recordToday.setUpdated(new Date());

            updateById(recordToday);
        }


    }
}
